<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col
        :span="3"
        class="left">
        <nuxt-link to="/"><img
          src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png"
          alt="美团"></nuxt-link>
      </el-col>
      <el-col
        :span="15"
        class="center">
        <div class="wrapper">
          <el-input
            v-model="keyword"
            placeholder="搜索商家或地点"
            @focus="focus"
            @blur="blur"
            @input="search"
          />
          <el-button
            class="el-button el-button--primary"
            @click="search"><i class="el-icon-search"/></el-button>
          <dl
            v-show="showHotPlace"
            class="hotPlace"
          >
            <dt>热门搜索</dt>
            <dd
              v-for="( hot , idx) in $store.state.home.hotPlace"
              :key="idx">
              <a href="/">{{ hot.name }}</a>
            </dd>
          </dl>
          <dl
            v-show="showSearchList"
            class="searchList"
          >
            <dd
              v-for="( search , idx) in searchList"
              :key="idx"
            >
              <a :href="'/product?keyword='+search.name ">{{ search.name }}</a>
            </dd>
          </dl>
        </div>
        <p class="suggest">
          <template v-for="( hot , idx) in $store.state.home.hotPlace">
            <a
              :key="idx"
              :href="'/product?keyword='+hot.name ">{{ hot.name }}</a>
          </template>
        </p>
        <ul class="nav">
          <li>
            <nuxt-link
              to="/"
              class="takeout">美团外卖
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="movie">猫眼电影
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="hotel">美团酒店
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="apartment">民宿/公寓
            </nuxt-link>
          </li>
          <li>
            <nuxt-link
              to="/"
              class="business">商家入驻
            </nuxt-link>
          </li>
        </ul>
      </el-col>
      <el-col
        :span="6"
        class="right">
        <ul class="security">
          <li>
            <i class="refund"/>
            <p class="txt">随时退</p>
          </li>
          <li>
            <i class="single"/>
            <p class="txt">不满意免单</p>
          </li>
          <li>
            <i class="overdue"/>
            <p class="txt">过期退</p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash'

export default {
  name: 'SearchBar',
  data() {
    return {
      keyword: '',
      isFocus: false,
      searchList: []
    }
  },
  computed: {
    showHotPlace() {
      return this.isFocus && !this.keyword
    },
    showSearchList() {
      return this.isFocus && this.keyword
    }
  },
  methods: {
    focus() {
      this.isFocus = true
    },
    blur() {
      setTimeout(() => (this.isFocus = false), 300)
    },
    search: _.debounce(async function() {
      const {
        status,
        data: { top }
      } = await this.$axios.get('/search/top', {
        params: {
          name: this.keyword,
          city: this.$store.state.geo.position.city
        }
      })
      if (status === 200) {
        this.searchList = top
      }
    }, 300)
  }
}
</script>

<style lang="scss" scoped>
</style>
